package Day017_缓冲流_转换流_序列化流_打印流.ZuoYe;

import java.io.*;

/**
 * @author zhangyan
 * @date 2020/5/9 20:38
 */
public class Test9 {
    public static void main(String[] args) throws Exception {

        //创建学生对象
        Student stu1 = new Student("张三", 18, "男");
        Student stu2 = new Student("李四", 20, "女");
        Student stu3 = new Student("王五", 22, "男");

        //创建序列化流,关联目的地文件路径
        FileOutputStream fos = new FileOutputStream("Demo02\\ccc\\stu.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fos);

        //写出对象
        oos.writeObject(stu1);
        oos.writeObject(stu2);
        oos.writeObject(stu3);

        //关流
        oos.close();

        //创建反序列化流
        FileInputStream fis = new FileInputStream("Demo02\\ccc\\stu.txt");
        ObjectInputStream ois = new ObjectInputStream(fis);

        //循环读取对象
        Student student;
        while ((student = (Student) ois.readObject()) != null) {
            System.out.println(student);
        }
        //关流
        ois.close();
    }
}
